package lib;

import java.sql.*;
import java.util.List;

public abstract class ExecuteLocal {
    public Connection connection;
    public abstract void call() throws Exception;

    public int executeUpdate(String sql, Object ... params) throws SQLException {
        return executeUpdate(connection, sql, params);
    }

    public int[] executeBatch(String sql, List<Object[]> paramsList) throws SQLException {
        return executeBatch(connection, sql, paramsList);
    }

    public int[] executeBatchByOne(String sql, Object ... params) throws SQLException {
        return executeBatchByOne(connection, sql, params);
    }

    public long executeUpdateResponse(String sql, Object ... params) throws SQLException {
        return executeUpdateResponse(connection, sql, params);
    }

    public static int executeUpdate(Connection connection, String sql, Object ... params) throws SQLException {
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            int index = 0;
            for (Object param : params) {
                pst.setObject(++index, param);
            }
            return pst.executeUpdate();
        } finally {
        	MySQLLoaclHelper.close(pst);
        }
    }

    public int[] executeBatch(Connection connection, String sql, List<Object[]> paramsList) throws SQLException {
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            for (Object[] params : paramsList) {
                int index = 0;
                for (Object param : params) {
                    pst.setObject(++index, param);
                }
                pst.addBatch();
            }
            return pst.executeBatch();
        } finally {
        	MySQLLoaclHelper.close(pst);
        }
    }

    public int[] executeBatchByOne(Connection connection, String sql, Object ... params) throws SQLException {
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            for (Object param : params) {
                pst.setObject(1, param);
                pst.addBatch();
            }
            return pst.executeBatch();
        } finally {
        	MySQLLoaclHelper.close(pst);
        }
    }

    public long executeUpdateResponse(Connection connection, String sql, Object ... params) throws SQLException {
        PreparedStatement pst = null;
        try {
            pst = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            int index = 0;
            for (Object param : params) {
                pst.setObject(++index, param);
            }
            pst.executeUpdate();
            ResultSet rs = pst.getGeneratedKeys();
            rs.next();
            return rs.getLong(1);
        } finally {
        	MySQLLoaclHelper.close(pst);
        }
    }
}
